package com.spgf.controller;

import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.archtecture.model.exceptions.NegocioException;
import com.archtecture.model.patterns.PadraoActivityMnt;
import com.archtecture.model.patterns.PadraoRNAb;
import com.spgf.model.entities.CategoriaModel;
import com.spgf.model.rn.CategoriaRN;
import com.spgf.view.utils.AdapterListViewCategorias;
import com.spgf.view.utils.DialogManutencao;

public class CategoriaMntActivity extends PadraoActivityMnt<CategoriaModel> {

	private List<CategoriaModel>	listaModel;

	// private Spinner spnCategoria;

	// private CategoriaModel model;

	private ListView				listview;

	@SuppressLint("SimpleDateFormat")
	public void atualizarLista() throws NegocioException {

		listaModel = null;
		listview.setAdapter(new AdapterListViewCategorias(this, getListaModel()));
	}

	// @SuppressWarnings({"unchecked", "rawtypes"})
	@Override
	protected void construirTela(Bundle savedInstanceState) throws NegocioException {
		setContentView(R.layout.activity_categoria_mnt);

		listview = (ListView) findViewById(R.id.lst_categoria_mnt_resultado);
		listview.setScrollbarFadingEnabled(true);
		listview.setOnItemLongClickListener(this);

		// spnCategoria = (Spinner) findViewById(R.id.spn_categoria_mnt_categoria);
		// spnCategoria.setAdapter(new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, new CategoriaRN(this).popularCombo(
		// new CategoriaModel(), true)));
		// spnCategoria.setOnItemSelectedListener(this);

		atualizarLista();

	}

	protected boolean executarLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {

		if (pParent.getId() == R.id.lst_categoria_mnt_resultado) {

			new DialogManutencao(this, getListaModel().get(pPosition)).show();
			return true;
		}

		return false;

	};

	// @Override
	// protected void executarSelecao(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {
	// super.executarSelecao(pParent, pView, pPosition, pID);
	//
	// if (pParent.getId() == R.id.spn_categoria_mnt_categoria) {
	// atualizarLista();
	// }
	// }

	@Override
	@SuppressWarnings({"rawtypes"})
	public Class getControleCadastro() {
		return CategoriaCadActivity.class;
	}

	public List<CategoriaModel> getListaModel() throws NegocioException {

		// if (listaModel == null) {
		// final CategoriaModel categoria = new CategoriaModel();
		// categoria.setCategoriaSuperior((CategoriaModel) spnCategoria.getSelectedItem());

		listaModel = new CategoriaRN(this).executarFindAll(new CategoriaModel());
		// }

		return listaModel;
	}
	
	@Override
	protected PadraoRNAb<CategoriaModel> getRN() {
		return new CategoriaRN(this);
	}
}
